﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace Tsanie.DmPoster {
    public enum Level {
        Guest = 0,
        Member = 10000,
        Porter = 15000,
        Commenter = 20000,
        Vip = 25000,
        Major = 30000,
        Admin = 32000
    }
    public class Config {
        internal static readonly string c_AppCaption = "BiliBili 弹幕助手 v"
            + Assembly.GetExecutingAssembly().GetName().Version.Major + "."
            + Assembly.GetExecutingAssembly().GetName().Version.Minor;
        internal static readonly string c_AppPath;

        private const string c_ConfigFile = "\\Config.xml";
        private static XmlDocument _config = null;

        internal static string m_UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 DmPoster";
        internal static string m_HttpBase = "http://www.bilibili.tv";
        internal static string m_Cookie = null;
        internal static Level m_Level = Level.Guest;
        internal static float m_CacheSeconds = 30f;
        internal static string m_Fontname = "simhei";
        internal static float m_playTime = 0.4f;
        internal static int m_fontsize = 25;
        internal static int m_color = 16777215;
        internal static int m_mode = 1;
        internal static Keys m_Edit = Keys.Shift | Keys.E;
        internal static Keys m_EditTime = Keys.T;
        internal static Keys m_EditColor = Keys.C;
        internal static Keys m_EditSize = Keys.F;
        internal static Keys m_EditMsg = Keys.S;
        internal static int m_Alpha = 255;

        static Config() {
            c_AppPath = Application.ExecutablePath.Substring(0,
                Application.ExecutablePath.LastIndexOf('\\'));
            if (!File.Exists(c_AppPath + c_ConfigFile)) {
                CreateConfig();
            }
            _config = new XmlDocument();
            _config.Load(c_AppPath + c_ConfigFile);
        }
        internal static string EnsureHttp(string http) {
            http = http.ToLower();
            if (!http.StartsWith("http://"))
                http = "http://" + http;
            return http;
        }
        internal static void SetValue(string key, string value) {
            if (key == "HttpBase") {
                m_HttpBase = EnsureHttp(value);
            } else if (key == "CacheSeconds") {
                m_CacheSeconds = float.Parse(value);
            } else if (key == "Fontname") {
                m_Fontname = value;
            } else if (key == "PlayTime") {
                m_playTime = float.Parse(value);
            } else if (key == "Fontsize") {
                m_fontsize = int.Parse(value);
            } else if (key == "Color") {
                m_color = int.Parse(value);
            } else if (key == "Mode") {
                m_mode = int.Parse(value);
            } else if (key == "EditKey") {
                m_Edit = (Keys)int.Parse(value);
            } else if (key == "EditKeyTime") {
                m_EditTime = (Keys)int.Parse(value);
            } else if (key == "EditKeyColor") {
                m_EditColor = (Keys)int.Parse(value);
            } else if (key == "EditKeySize") {
                m_EditSize = (Keys)int.Parse(value);
            } else if (key == "EditKeyMsg") {
                m_EditMsg = (Keys)int.Parse(value);
            } else if (key == "Alpha") {
                m_Alpha = int.Parse(value);
            }
            XmlNode node = _config.SelectSingleNode("/configuration/" + key);
            if (node == null) {
                node = _config.CreateElement(key);
                node.AppendChild(_config.CreateCDataSection(value));
                _config.SelectSingleNode("/configuration").AppendChild(node);
            } else {
                node.RemoveAll();
                node.AppendChild(_config.CreateCDataSection(value));
            }
            Save();
        }
        internal static string GetValue(string key) {
            XmlNode node = _config.SelectSingleNode("/configuration/" + key);
            if (node == null)
                return null;
            string val = (string.IsNullOrEmpty(node.InnerText) ? null : node.InnerText);
            if (key == "HttpBase")
                val = EnsureHttp(val);
            return val;
        }
        internal static void Save() {
            _config.Save(c_AppPath + c_ConfigFile);
        }
        internal static void LoadConfig() {
            string value = GetValue("UserAgent");
            if (value != null)
                m_UserAgent = value;

            m_Cookie = GetValue("Cookie");

            value = GetValue("HttpBase");
            if (value != null)
                m_HttpBase = EnsureHttp(value);

            value = GetValue("CacheSeconds");
            if (value != null)
                float.TryParse(value, out m_CacheSeconds);
            if (m_CacheSeconds < 0)
                m_CacheSeconds = 30f;

            value = GetValue("Fontname");
            if (value != null)
                m_Fontname = value;

            value = GetValue("PlayTime");
            if (value != null)
                float.TryParse(value, out m_playTime);
            // 如果播放时间不合法
            if (m_playTime < 0)
                m_playTime = 0.4f;

            value = GetValue("Fontsize");
            if (value != null)
                int.TryParse(value, out m_fontsize);
            // 如果字号大小不合法
            if (m_fontsize < 0)
                m_fontsize = 25;

            value = GetValue("Color");
            if (value != null)
                int.TryParse(value, out m_color);

            value = GetValue("Mode");
            if (value != null)
                int.TryParse(value, out m_mode);
            // 如果模式不合法
            if (m_mode != 1 && m_mode < 4 && m_mode > 7)
                m_mode = 1;

            int key;
            value = GetValue("EditKey");
            if (value != null) {
                int.TryParse(value, out key);
                m_Edit = (Keys)key;
            }
            value = GetValue("EditTimeKey");
            if (value != null) {
                int.TryParse(value, out key);
                m_EditTime = (Keys)key;
            }
            value = GetValue("EditColorKey");
            if (value != null) {
                int.TryParse(value, out key);
                m_EditColor = (Keys)key;
            }
            value = GetValue("EditSizeKey");
            if (value != null) {
                int.TryParse(value, out key);
                m_EditSize = (Keys)key;
            }
            value = GetValue("EditMsgKey");
            if (value != null) {
                int.TryParse(value, out key);
                m_EditMsg = (Keys)key;
            }
            value = GetValue("Alpha");
            if (value != null) {
                int.TryParse(value, out key);
                m_Alpha = key;
            }
        }
        private static void CreateConfig() {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(c_AppPath + c_ConfigFile, settings);
            writer.WriteStartElement("configuration");
            writer.WriteStartElement("Cookie");
            writer.WriteCData(Config.m_Cookie);
            writer.WriteEndElement();
            writer.WriteStartElement("CacheSeconds");
            writer.WriteCData(Config.m_CacheSeconds.ToString());
            writer.WriteEndElement();
            writer.WriteStartElement("Fontname");
            writer.WriteCData(Config.m_Fontname);
            writer.WriteEndElement();
            writer.WriteStartElement("PlayTime");
            writer.WriteCData(Config.m_playTime.ToString());
            writer.WriteEndElement();
            writer.WriteStartElement("Fontsize");
            writer.WriteCData(Config.m_fontsize.ToString());
            writer.WriteEndElement();
            writer.WriteStartElement("Color");
            writer.WriteCData(Config.m_color.ToString());
            writer.WriteEndElement();
            writer.WriteStartElement("Mode");
            writer.WriteCData(Config.m_mode.ToString());
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();
            writer.Close();
        }
    }
}
